package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.DataFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;
import com.pay.ioopos.ScanListener;
import com.pay.ioopos.fragment.DomainFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;

public class DomainActivity extends AbstractActivity implements ScanListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_domain);
        DomainFragment scanFragment = new DomainFragment();
        scanFragment.setListener(this);
        setMainFragment(scanFragment);
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
            Intent intent = new Intent(this, ConnectActivity.class);
            startActivity(intent);
        }
        return false;
    }

    @Override
    public void onScan(String content) {
        String[] params = content.split("\\|");
        DataFactory.getDataStore().setServerUrl(params[0]);
        if (params.length > 1) {
            DataFactory.getDataStore().setTransKey(params[1]);
        }
        setMainFragment(new TipVerticalFragment(SUCCESS, "支付参数配置成功"));
    }
}
